//
//  SelectedView.m
//  SelectedView
//
//  Created by hcios on 2017/6/25.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width

#import "SelectedView.h"

@interface SelectedView ()

@property (nonatomic, strong) NSArray * buttonName;

@property (nonatomic, strong) UILabel * underLine;

@property (nonatomic, strong) UILabel * downLine;

@property (nonatomic, strong) UIButton * selectButton;

@property (nonatomic, strong) NSArray * controllers;

@property (nonatomic, strong) UIView * selectedView;

@property (nonatomic, strong) UIScrollView * selectedScrollV;

@end

@implementation SelectedView

-(instancetype)initWithFrame:(CGRect)frame buttonName:(NSArray *)buttonName controllers:(NSArray *)controllers parentController:(UIViewController *)parentC {

    if (self = [super initWithFrame:frame]) {
        self.buttonName = buttonName;
        self.controllers = controllers;
        //创建selectedView
        self.selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 48)];
        self.selectedView.tag = 50;
        [self addSubview:self.selectedView];
        
        //创建selectedScrollV
        self.selectedScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 48, SCREEN_WIDTH, frame.size.height)];
        self.selectedScrollV.contentSize = CGSizeMake(SCREEN_WIDTH * self.controllers.count, 0);
        self.selectedScrollV.showsHorizontalScrollIndicator = NO;
        self.selectedScrollV.pagingEnabled = YES;
        self.selectedScrollV.bounces = YES;
        self.selectedScrollV.delegate = self;
        [self addSubview:self.selectedScrollV];
        
        //创建ViewController并添加到selectedScrollV上
        for (int i = 0; i < self.controllers.count; i++) {
            
            UIViewController * VC = self.controllers[i];
            VC.view.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, frame.size.height);
            [self.selectedScrollV addSubview:VC.view];
            [parentC addChildViewController:VC];
            //当我们向我们的视图控制器容器（就是父视图控制器，它调用addChildViewController方法加入子视图控制器，它就成为了视图控制器的容器）中添加（或者删除）子视图控制器后，必须调用该方法，告诉iOS，已经完成添加（或删除）子控制器的操作。
            [VC didMoveToParentViewController:parentC];//子视图交换时使用
        }
        
        //创建selectedButton和line
        for (int i = 0; i < self.controllers.count; i++) {
            
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(i * (SCREEN_WIDTH / self.controllers.count), 0, SCREEN_WIDTH / self.controllers.count, 48);
            button.tag = i;
            [button setTitle:self.buttonName[i] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
            button.titleLabel.font = [UIFont systemFontOfSize:20];
            if (i == 0) {
                
                button.selected = YES;
                self.selectButton = button;
                button.titleLabel.font = [UIFont systemFontOfSize:23];
            }else {
            
                button.selected = NO;
            }
            [self.selectedView addSubview:button];
        }
        
        //添加下划线
        self.underLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 46, SCREEN_WIDTH / self.controllers.count, 1.5)];
        self.underLine.backgroundColor = [UIColor blueColor];
        self.underLine.tag = 70;
        [self.selectedView addSubview:self.underLine];
        
        self.downLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 47.5, SCREEN_WIDTH, 0.5)];
        self.downLine.backgroundColor = [UIColor grayColor];
        [self.selectedView addSubview:self.downLine];
    }
    
    return self;
}

- (void)Click:(UIButton *)sender{
    
    self.selectButton.titleLabel.font = [UIFont systemFontOfSize:20];
    self.selectButton.selected = NO;
    self.selectButton = sender;
    self.selectButton.selected = YES;
    self.selectButton.titleLabel.font = [UIFont systemFontOfSize:23];
    [UIView animateWithDuration:0.2 animations:^{
        
        CGPoint frame = self.underLine.center;
        frame.x = SCREEN_WIDTH / (self.controllers.count * 2) + (SCREEN_WIDTH / self.controllers.count) * (sender.tag);
        self.underLine.center = frame;
    }];
    
    [self.selectedScrollV setContentOffset:CGPointMake((sender.tag) * SCREEN_WIDTH, 0) animated:YES];
    
}



#pragma mark  UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [UIView animateWithDuration:0.2 animations:^{
        
        CGPoint frame = self.underLine.center;
        frame.x = SCREEN_WIDTH / (self.controllers.count * 2) + (SCREEN_WIDTH / self.controllers.count) * (self.selectedScrollV.contentOffset.x/SCREEN_WIDTH);
        self.underLine.center = frame;
        
    }];
    
    UIButton *button = (UIButton *)[self.selectedView viewWithTag:self.selectedScrollV.contentOffset.x / SCREEN_WIDTH];
    self.selectButton.titleLabel.font = [UIFont systemFontOfSize:20];
    self.selectButton.selected = NO;
    self.selectButton = button;
    self.selectButton.selected = YES;
    self.selectButton.titleLabel.font = [UIFont systemFontOfSize:23];
    
}







@end
